//
// Created by 11571 on 2024/2/11.
//

#include "lv_page_main.h"
#include "lv_page_menu.h"

static lv_obj_t * label_time;
static lv_obj_t * label_date;
static lv_obj_t * label_weather;

static lv_obj_t * btn1;

static lv_style_t style_txt;
static lv_style_t style_btn1;
static lv_style_t style_btn1_clicked;

static lv_timer_t * update_info_tmr;

extern lv_ft_info_t font_info_72;
extern lv_ft_info_t font_info_40;
extern lv_ft_info_t font_info_20;

lv_obj_t * ui_page_main;

static void ui_event_cb(lv_event_t * event)
{
    lv_obj_t * target    = lv_event_get_target(event);
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
        if(target == btn1) {
            lv_page_menu_create();
            lv_page_main_del();
            lv_scr_load_anim(ui_page_menu, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, true);
        }
    }
}

static void update_info_tmr_cb(lv_timer_t * timer)
{
    time_t t        = time(NULL);
    struct tm * st  = localtime(&t);
    char * wday_txt = NULL;

    lv_label_set_text_fmt(label_time, "%02d:%02d:%02d", st->tm_hour, st->tm_min, st->tm_sec);
    switch(st->tm_wday) {
        case 0: wday_txt = "周日"; break;
        case 1: wday_txt = "周一"; break;
        case 2: wday_txt = "周二"; break;
        case 3: wday_txt = "周三"; break;
        case 4: wday_txt = "周四"; break;
        case 5: wday_txt = "周五"; break;
        case 6: wday_txt = "周六"; break;
        default: wday_txt = "未知"; break;
    }
    lv_label_set_text_fmt(label_date, "%d月%d日 %s", st->tm_mon + 1, st->tm_mday, wday_txt);
}

void lv_page_main_create(void)
{
    ui_page_main = lv_obj_create(NULL);

    lv_obj_set_style_bg_img_opa(ui_page_main, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_img_src(ui_page_main, "A:/panel_bg.sjpg", 0);

    if(!style_txt.prop_cnt) {
        lv_style_init(&style_txt);
        lv_style_set_text_color(&style_txt, lv_color_white());
        lv_style_set_text_align(&style_txt, LV_TEXT_ALIGN_CENTER);
        lv_style_set_width(&style_txt, 480);
    }

    if(!style_btn1.prop_cnt) {
        lv_style_init(&style_btn1);
        lv_style_set_pad_ver(&style_btn1, 8);
        lv_style_set_pad_hor(&style_btn1, 16);
        lv_style_set_radius(&style_btn1, 12);
        lv_style_set_bg_color(&style_btn1, lv_color_make(0, 191, 153));
        lv_style_set_bg_opa(&style_btn1, LV_OPA_COVER);
        lv_style_set_width(&style_btn1, LV_SIZE_CONTENT);
        lv_style_set_height(&style_btn1, LV_SIZE_CONTENT);
    }

    if(!style_btn1_clicked.prop_cnt) {
        lv_style_init(&style_btn1_clicked);
        lv_style_set_bg_color(&style_btn1_clicked, lv_color_make(0, 156, 125));
    }

    label_time = lv_label_create(ui_page_main);
    lv_obj_set_style_text_font(label_time, font_info_72.font, 0);
    lv_obj_add_style(label_time, &style_txt, 0);
    lv_label_set_text_static(label_time, "12:56:12");
    lv_obj_align(label_time, LV_ALIGN_TOP_LEFT, 0, 119);

    label_date = lv_label_create(ui_page_main);
    lv_obj_set_style_text_font(label_date, font_info_40.font, 0);
    lv_obj_add_style(label_date, &style_txt, 0);
    lv_label_set_text_static(label_date, "2月11日 周日");
    lv_obj_align(label_date, LV_ALIGN_TOP_LEFT, 0, 205);

    label_weather = lv_label_create(ui_page_main);
    lv_obj_set_style_text_font(label_weather, font_info_20.font, 0);
    lv_obj_add_style(label_weather, &style_txt, 0);
    lv_label_set_text_static(label_weather, "宁波 13℃ 晴天");
    lv_obj_align(label_weather, LV_ALIGN_TOP_LEFT, 0, 264);

    lv_obj_t * label_btntxt;
    btn1 = lv_btn_create(ui_page_main);
    lv_obj_remove_style_all(btn1);
    lv_obj_add_style(btn1, &style_btn1, 0);
    lv_obj_add_style(btn1, &style_btn1_clicked, LV_STATE_PRESSED);
    lv_obj_add_event_cb(btn1, ui_event_cb, LV_EVENT_CLICKED, NULL);

    label_btntxt = lv_label_create(btn1);
    lv_obj_set_style_text_font(label_btntxt, font_info_20.font, 0);
    lv_obj_set_style_text_color(label_btntxt, lv_color_white(), 0);
    lv_label_set_text(label_btntxt, "菜单");
    lv_obj_center(label_btntxt);
    lv_obj_set_pos(btn1, 203, 353);

    update_info_tmr = lv_timer_create(update_info_tmr_cb, 500, NULL);
}

void lv_page_main_del(void)
{
    lv_timer_del(update_info_tmr);
}